/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ec.com.farmaciassantamartha.comprobantes.portal.mb;

import ec.com.farmaciassantamartha.comprobantes.portal.bean.Comprobante;
import ec.com.farmaciassantamartha.comprobantes.portal.sql.ComprobanteSQL;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Maestria
 */
@ManagedBean
@RequestScoped
public class ComprobanteMB {

    private List<Comprobante> lstComprobantes;
    private Comprobante comprobanteSeleccionado;
    private StreamedContent file;

    /**
     * Creates a new instance of ComprobanteMB
     */
    public ComprobanteMB() {
        HttpServletRequest request = (HttpServletRequest) javax.faces.context.FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpSession session = request.getSession();
        UsuarioMB userBean = (UsuarioMB) session.getAttribute("usuarioMB");
        lstComprobantes = ComprobanteSQL.listarComprobantes(userBean.getId());
    }

    public List<Comprobante> getLstComprobantes() {
        return lstComprobantes;
    }

    public void setLstComprobantes(List<Comprobante> lstComprobantes) {
        this.lstComprobantes = lstComprobantes;
    }

    public Comprobante getComprobanteSeleccionado() {
        return comprobanteSeleccionado;
    }

    public void setComprobanteSeleccionado(Comprobante comprobanteSeleccionado) {
        try {
            this.comprobanteSeleccionado = comprobanteSeleccionado;
            InputStream stream = new FileInputStream(comprobanteSeleccionado.getPathXml());
//            new DefaultStreamedContent
            file = new DefaultStreamedContent(stream, "text/plain", comprobanteSeleccionado.getClaveAcceso() + ".xml");
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public StreamedContent getFile() {
        return file;
    }

    public void setFile(StreamedContent file) {
        this.file = file;
    }
}
